package com.autoloadcache.demo.autoloadcacheDemo.service;
import	java.util.ArrayList;

import com.autoloadcache.demo.autoloadcacheDemo.pojo.User;
import com.jarvis.cache.annotation.Cache;
import com.jarvis.cache.annotation.CacheDelete;
import com.jarvis.cache.annotation.CacheDeleteKey;
import com.jarvis.cache.type.CacheOpType;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * @Author mars
 * @Description 注解说明
 * - @Cache
 * - @ExCache
 * - @CacheDelete 删除缓存注解
 * - @CacheDeleteKey 生成删除缓存key注解
 * - @CacheDeleteTransactional 事务环境中批量删除缓存注解
 * - @LocalCache 本地缓存注解
 * @Date
 * @return
 */
@Service
public class UserService {


    @Cache(expire = 60,expireExpression = " null==#retVal ? 30 : 60" ,key=" 'getUser1:' + #args[0]")
    public User getUser1 (int userId) {
        if (userId==1) {
            return null;
        } else {
            System.out.println("执行该方法，说明没有缓存，没有缓存才会走到这里");
            return new User(userId,"没有缓存_"+userId,"psw_"+userId);
        }
    }

    // 设置key="user-all" ,expire="过期时间"
    @Cache(expire = 60,key="user-all",autoload = true)
    public List<User> getUserList () {
        System.out.println("执行该方法，说明没有缓存，没有缓存才会走到这里");
        List<User> list = new ArrayList<User> ();
        list.add(new User(1,"小明","12345678"));
        list.add(new User(2,"lily","12345678"));
        list.add(new User(3,"steven","12345678"));
        return list;
    }

    // 设置key="user.getUserById:userId" ,expire="过期时间"
    @Cache(expire = 60,key = " 'user.getUserById:' + #args[0]")
    public User getUserById(String userId) {
        System.out.println("执行该方法，说明没有缓存，没有缓存才会走到这里");
        return new User(Integer.parseInt(userId),"mars","543212a");
    }

    @Cache(expire=600, key="'user'+#retVal.id", opType= CacheOpType.WRITE)
    public User save(User user) {
        System.out.println("save user=" + user);
        return user;
    }
}
